package com.rcloud.server.sealtalk.task;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.rcloud.server.sealtalk.domain.UserDocList;
import com.rcloud.server.sealtalk.domain.Users;
import com.rcloud.server.sealtalk.exception.ServiceException;
import com.rcloud.server.sealtalk.manager.FaceAuthManager;
import com.rcloud.server.sealtalk.service.UserDocListService;
import com.rcloud.server.sealtalk.service.UsersService;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class CompanyAuthTask {
    @Autowired
    private UsersService usersService;

    @Autowired
    private FaceAuthManager faceAuthManager;

    @Autowired
    private UserDocListService docListService;

//    @Async
//    @Scheduled(cron = "0 0 2 * * ?")//每天凌晨2点执行一次
    public void licenseAuthTask() {
        //业务处理
        log.info("执行证件审核的定时任务---------------------开始" + new Date());
        Example example = new Example(Users.class);
        example.createCriteria()
                .andEqualTo("isReal", 1)
                .andEqualTo("payAuthStatus", 1)
                .andEqualTo("isFace", 1)
                .andEqualTo("isMerchant", 0);
        List<Users> usersList = usersService.getByExample(example);
        for (Users user : usersList) {
            try {
                faceAuthManager.authCert(user.getId());
            } catch (IOException | ServiceException e) {
                log.info("执行证件审核的定时任务出现异常:" + e.getMessage());
            }
        }
        log.info("执行证件审核的定时任务---------------------结束" + new Date());
    }


    /**
     * 认证用户的判断，时间如果是一年的则需要重新认证
     */
    @Async
    @Scheduled(cron = "0 0 1 * * ?")//每天凌晨1点执行一次
    public void queryUserExpireAuthTask() {
        DateTime date = DateUtil.date();
        //业务处理
        log.info("执行认证用户到期时间判断定时任务---------------------开始" + date);
        Example example = new Example(Users.class);
        example.createCriteria()
                .andEqualTo("isReal", 1)
                .andEqualTo("payAuthStatus", 1)
                .andEqualTo("isFace", 1)
                .andEqualTo("isMerchant", 1);
        List<Users> usersList = usersService.getByExample(example);
        for (Users user : usersList) {
            Date validDate = user.getAuthValidDate();
            //如果当前时间和到期时间做比较 ,到期认证状态重置
            if (validDate.getTime() < date.getTime()) {
                user.setIsMerchant(0);
                user.setPayAuthStatus(0);
                user.setIsFace(0);
                usersService.updateByPrimaryKeySelective(user);
                Example docExample = new Example(UserDocList.class);
                example.createCriteria()
                        .andEqualTo("userId", user.getId())
                        .andEqualTo("dataType", 3).orEqualTo("dataType", 4);
                List<UserDocList> byExample = docListService.getByExample(docExample);
                for (UserDocList doc : byExample) {
                    doc.setAuthStatus(0);
                    doc.setAuthTime(null);
                    docListService.updateByPrimaryKeySelective(doc);
                }
            }
        }
        log.info("执行认证用户到期时间判断的定时任务---------------------结束" + date.getTime());
    }


}
